/**
 * 
 */
package com.thor.palmmall.rest.sales.input;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.thor.commons.entity.BeanOperateInfo;
import com.thor.commons.exception.ThorServiceException;
import com.thor.commons.query.QueryResult;
import com.thor.commons.util.Assert;
import com.thor.commons.util.ConverterUtil;
import com.thor.palmmall.rest.common.AUserGroupLoadParam;
import com.thor.palmmall.rest.common.SUserGroupLoadParamConverter;
import com.thor.palmmall.rest.sales.input.converter.AContractProductsConverter;
import com.thor.palmmall.rest.sales.input.converter.APaymentsConverter;
import com.thor.palmmall.rest.sales.input.converter.ASalesInputConverter;
import com.thor.palmmall.rest.sales.input.converter.ASalesTotalDataConverter;
import com.thor.palmmall.rest.sales.input.converter.SProductsParamConverter;
import com.thor.palmmall.rest.sales.input.converter.SSalesInput4CreateConverter;
import com.thor.palmmall.rest.sales.input.converter.SSalesInputConditionConverter;
import com.thor.palmmall.rest.sales.input.converter.SSalesInputConverter;
import com.thor.palmmall.rest.sales.input.converter.SSalesInputQueryParamConverter;
import com.thor.palmmall.spi.basic.SBasicService;
import com.thor.palmmall.spi.basic.SContractProducts;
import com.thor.palmmall.spi.basic.SPayments;
import com.thor.palmmall.spi.common.SOperateInfoConverter;
import com.thor.palmmall.spi.common.SQueryResult;
import com.thor.palmmall.spi.common.SQueryResultConverter;
import com.thor.palmmall.spi.common.SResponse;
import com.thor.palmmall.spi.sales.input.SSalesInput;
import com.thor.palmmall.spi.sales.input.SSalesInputService;
import com.thor.palmmall.spi.sales.input.SSalesTotalData;

/**
 * @author NuoSa
 *
 * @since 1.0
 */
public class ASalesInputServiceImpl implements ASalesInputService {

  @Override
  public String create(ASalesInput salesInput, BeanOperateInfo operateInfo)
      throws ThorServiceException {
    Assert.assertArgumentNotNull(salesInput, "salesInput");
    Assert.assertArgumentNotNull(operateInfo, "operateInfo");
    Assert.assertArgumentNotNull(operateInfo.getOperator(), "operateInfo.getOperator()");
    Assert.assertArgumentNotNull(operateInfo.getTime(), "operateInfo.getTime()");

    SResponse<String> resp = sSalesInputService.create(
        SSalesInput4CreateConverter.getInstance().convert(salesInput),
        SOperateInfoConverter.getInstance().convert(operateInfo));
    if (resp.isSuccess())
      return resp.getBody();
    else
      throw new ThorServiceException("无法录入销售单：" + resp.getMessage());
  }

  @Override
  public ASalesInput get(String id, AUserGroupLoadParam param) throws ThorServiceException {
    SResponse<SSalesInput> resp = sSalesInputService.get(id,
        SUserGroupLoadParamConverter.getInstance().convert(param));
    if (resp.isSuccess())
      return ASalesInputConverter.getInstance().convert(resp.getBody());
    else
      throw new ThorServiceException("无法找到指定ID的销售单：" + resp.getMessage());
  }

  @Override
  public List<ASalesTotalData> getSalesTotal(ASalesInputCondition condition)
      throws ThorServiceException {
    SResponse<List<SSalesTotalData>> resp = sSalesInputService
        .getSalesTotal(SSalesInputConditionConverter.getInstance().convert(condition));
    if (resp.isSuccess())
      return ConverterUtil.convert(resp.getBody(), ASalesTotalDataConverter.getInstance());
    else
      throw new ThorServiceException("没有查询到销售数据：" + resp.getMessage());
  }

  @Override
  public String save(ASalesInput salesInput, BeanOperateInfo operateInfo)
      throws ThorServiceException {
    Assert.assertArgumentNotNull(salesInput, "salesInput");
    Assert.assertArgumentNotNull(operateInfo, "operateInfo");
    Assert.assertArgumentNotNull(operateInfo.getOperator(), "operateInfo.getOperator()");
    Assert.assertArgumentNotNull(operateInfo.getTime(), "operateInfo.getTime()");
    SResponse<String> resp = sSalesInputService.save(
        SSalesInputConverter.getInstance().convert(salesInput),
        SOperateInfoConverter.getInstance().convert(operateInfo));
    if (resp.isSuccess())
      return resp.getBody();
    else
      throw new ThorServiceException("无法保存销售单：" + resp.getMessage());

  }

  @Override
  public QueryResult<ASalesInput> query(ASalesInputQueryParam param) throws ThorServiceException {

    SResponse<SQueryResult<SSalesInput>> resp = sSalesInputService
        .query(SSalesInputQueryParamConverter.getInstance().convert(param));
    if (resp.isSuccess())
      return SQueryResultConverter.convert(resp.getBody(), ASalesInputConverter.getInstance());
    else
      throw new ThorServiceException("无法查询到销售单：" + resp.getMessage());
  }

  @Override
  public List<APayments> getAllPayments() throws ThorServiceException {
    SResponse<List<SPayments>> resp = sBasicService.getAllPayments();
    if (resp.isSuccess()) {
      return ConverterUtil.convert(resp.getBody(), APaymentsConverter.getInstance());
    } else {
      throw new ThorServiceException("没有查询到可用的付款方式：" + resp.getMessage());
    }
  }

  @Override
  public List<AContractProducts> products(AProductsParam param) throws ThorServiceException {
    SResponse<List<SContractProducts>> resp = sBasicService
        .products(SProductsParamConverter.getInstance().convert(param));
    if (resp.isSuccess()) {
      return ConverterUtil.convert(resp.getBody(), AContractProductsConverter.getInstance());
    } else {
      throw new ThorServiceException("没有查询到指定日期及合同下的使用中的商品列表：" + resp.getMessage());
    }
  }

  @Autowired
  private SSalesInputService sSalesInputService;
  @Autowired
  private SBasicService sBasicService;

}
